import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';

import '../../resources/colours.dart';
import 'qkd_text.dart';
import 'qkd_button.dart';

enum DialogStyle{
  alert,
  single,
  select,
  alertButton,
  singleButton
}

class QkdDialog extends StatefulWidget {
  final String title;
  final String? content;
  final Widget? contentWidget;
  final TextAlign? contentAlign;
  final String? cancelText;
  final String? confirmText;
  final int? confirmTime;
  final Axis buttonDirection;
  final DialogStyle style;
  final List<String>? selectList;

  ///双选框
  const QkdDialog.alert(this.title,
    {super.key,
    this.content,
    this.contentWidget,
    this.confirmText = '确定',
    this.cancelText = '取消',
    this.contentAlign,
    this.confirmTime})
    : buttonDirection = Axis.horizontal,
      selectList = null,
      style = DialogStyle.alert;

  ///双选框按钮风格
  const QkdDialog.alertBUTTON(this.title,
      {super.key,
      this.content,
      this.contentWidget,
      this.confirmText = '确定',
      this.cancelText = '取消',
      this.contentAlign,
      this.buttonDirection = Axis.horizontal,
      this.confirmTime})
      : selectList = null,
        style = DialogStyle.alertButton;

  ///单双选框按钮风格
  const QkdDialog.singleBUTTON(this.title,
    {super.key,
    this.content,
    this.contentWidget,
    this.confirmText = '确定',
    this.cancelText,
    this.contentAlign,
    this.confirmTime})
    : buttonDirection = Axis.horizontal,
      selectList = null,
      style = DialogStyle.singleButton;

  ///单双选框
  const QkdDialog.single(this.title,
      {super.key,
      this.content,
      this.contentWidget,
      this.confirmText = '确定',
      this.cancelText,
      this.contentAlign,
      this.confirmTime})
      : buttonDirection = Axis.horizontal,
        selectList = null,
        style = DialogStyle.single;

  ///多选框
  const QkdDialog.select(this.title,
      {super.key,
        this.content,
        this.contentWidget,
        this.cancelText = '取消',
        this.contentAlign,
        this.selectList})
      : confirmTime = null,
        confirmText = null,
        buttonDirection = Axis.vertical,
        style = DialogStyle.select;

  @override
  State<QkdDialog> createState() => _QkdDialogState();
}

class _QkdDialogState extends State<QkdDialog> {
  late int _count = widget.confirmTime??0;

  set count(int value) => setState(() => _count = value);

  @override
  void initState() {
    if(_count != 0) {
      Timer.periodic(
      const Duration(seconds: 1),
        (timer) {
          count = _count-1;
          if(_count == 0) timer.cancel();
        }
      );
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      alignment: Alignment.center,
      backgroundColor: Colours.white,
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(12))),
      child: PopScope(
        canPop: false,
        child: SizedBox(
            width: 280,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  padding: const EdgeInsets.fromLTRB(16,24,16,26),
                  constraints: const BoxConstraints(minHeight: 110),
                  alignment: Alignment.center,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      if(widget.title != "")
                      QkdText(widget.title,
                        textAlign: TextAlign.center,
                        fontSize: 16,
                        color: Colours.text,
                        fontWeight: FontWeight.w500
                      ),
                      if (widget.content != null || widget.contentWidget != null) ...[
                        if(widget.title != "") Container(height: 16),
                        widget.content == null
                        ? DefaultTextStyle(
                          style: const TextStyle(
                            fontSize: 14, color: Colours.grey66),
                          child: widget.contentWidget!
                        )
                        : QkdText(widget.content!.split("").join("\u200B"),
                          textAlign: widget.contentAlign ?? TextAlign.left,
                          color: Colours.grey66
                        )
                      ]
                    ],
                  ),
                ),
                bottom(),
              ],
            )),
      ),
    );
  }

  Widget bottom(){
    switch(widget.style){
      case DialogStyle.alert:case DialogStyle.single:
        return Container(
          alignment: Alignment.center,
          decoration: const BoxDecoration(
            border: Border(
              top: BorderSide(
                color: Colours.greyEE, width: 0.5
              )
            )
          ),
          height: 44,
          child: Row(
            children: [
              if(widget.cancelText != null)...[
                Expanded(
                  child: QkdText(widget.cancelText!,
                    textAlign: TextAlign.center,
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    color: Colours.grey66
                  ).center.tap(
                    () => Navigator.pop(context,false)
                  )
                ),
                Container(width: 0.5, color: Colours.greyEE)
              ],
              Expanded(
                child: QkdText("${widget.confirmText}${_count.hasValue?"($_count秒)":""}",
                  textAlign: TextAlign.center,
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                  color: Colours.main.withOpacity(_count.hasValue?0.5:1)
                ).center.tap(
                  () => _count.hasValue?null:Navigator.pop(context,true)
                )
              )
            ]
          ).paddingSymmetric(horizontal: 16),
        );
      case DialogStyle.select:
        return Column(
          children: List.generate(
            (widget.selectList?.length??-1) + 1,
              (index) => Container(
              height: 44,
              alignment: Alignment.center,
              decoration: const BoxDecoration(
                border: Border(
                  top: BorderSide(color: Colours.greyEE, width: 0.5)
                )
              ),
              child: QkdText(
                index == widget.selectList?.length
                  ? widget.cancelText! : widget.selectList?[index],
                fontSize: 16,
                color: index == widget.selectList?.length
                  ? Colours.grey66 : Colours.main,
                fontWeight: FontWeight.w500
              )
            ).tap(
              () => Navigator.pop(context,
                index == widget.selectList?.length ? false : index
              )
            )
          )
        ).paddingSymmetric(horizontal: 16);
      case DialogStyle.alertButton:case DialogStyle.singleButton:
        if(widget.buttonDirection == Axis.horizontal){
          return Row(children: [
            if (widget.cancelText != null)...[
              Flexible(
                fit: FlexFit.tight,
                child: QkdButton(
                  borderRadius: 4,
                  onPressed: () => Navigator.pop(context,false),
                  backgroundColor: Colours.greyF2,
                  foregroundColor: Colours.grey66,
                  text: widget.cancelText,
                ),
              ),
              const SizedBox(width: 10),
            ],
            Flexible(
              fit: FlexFit.tight,
              child: QkdButton(
                borderRadius: 4,
                onPressed: () => Navigator.pop(context,true),
                backgroundColor: Colours.main,
                foregroundColor: Colours.white,
                text: widget.confirmText,
              ),
            )
          ]).paddingOnly(bottom: 24,left: 16,right: 16);
        }else{
          return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              QkdButton.large(
                borderRadius: 4,
                onPressed: () => Navigator.pop(context,true),
                backgroundColor: Colours.main,
                foregroundColor: Colours.white,
                text: widget.confirmText,
              ),
              QkdButton.large(
                borderRadius: 4,
                onPressed: () => Navigator.pop(context,false),
                foregroundColor: Colours.grey66,
                backgroundColor: Colours.white,
                text: widget.cancelText,
              ),
              const SizedBox(height: 4)
            ]
          ).paddingSymmetric(horizontal: 16);
        }
    }
  }
}
